# -*- coding: utf-8 -*-

# graphic-pppoe-client - Easy graphic networking manager
#
# Author: GoTLiuM InSPiRiT <gotlium@gmail.com>
#
# Copyright 2010, GoTLiuM InSPiRiT <gotlium@gmail.com>.
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2, as published
# by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranties of
# MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program.  If not, see <http://www.gnu.org/licenses/>.

from libraries.net import getIfInfo
from models.__init__ import getIfnameByConnId
from libraries.net import addRoute, delRoute, getAvailableIfaces, getIfRoutes

class Routes(object):

	def setRoutes(self, connId):
		ifName = getIfnameByConnId(connId)
		for row in self.selectRoutes(connId):
			try:
				addRoute(
					ifName,
					row['destination'],
					row['gateway'],
					row['netmask']
				)
			except  Exception, msg:
				print msg
		return True

	def delRoutes(self, connId):
		ifName = getIfnameByConnId(connId)
		for row in self.selectRoutes(connId):
			try:
				delRoute(
					ifName,
					row['destination'],
					row['gateway'],
					row['netmask']
				)
			except:
				pass
		return True

	def getRoutes(self, connId):
		return getIfRoutes(getIfnameByConnId(connId))

	def flushAllRoutes(self, connId):
		for iface in getAvailableIfaces():
			for row in getIfRoutes(iface):
				try:
					delRoute(
						iface,
						row['dest'],
						row['gateway'],
						row['netmask']
					)
				except:
					pass
		return True
